home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 August / Macworld (1999-08).dmg / Shareware World / Info / For Developers / MADE 1.4.0 / Essentials / Essential Memory.c < prev    next >
Text File  |  1999-05-26  |  6KB  |  238 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /*                                                                   */
  3. /*        MADE - Macintosh Application Development Essentials        */
  4. /*        ---------------------------------------------------        */
  5. /*           (c) Sig Software, http://www.sigsoftware.com/           */
  6. /*                                                                   */
  7. /* These files can only be used for experimental purposes. To obtain */
  8. /* fully commented code, source code for the functions in Essential  */
  9. /*   Extras.h and permission for usage in final projects, you must   */
  10. /*    purchase a license. See documentation for more information.    */
  11. /*                                                                   */
  12. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  13. /*                                                                   */
  14. /*  Essential Memory.c                                               */
  15. /*  ------------------                                               */
  16. /*                                                                   */
  17. /*  Memory allocation, deallocation and initialisation.              */
  18. /*                                                                   */
  19. /*  Version 1.0.0 - 10th November 1996                               */
  20. /*  Version 1.0.1 - 4th June 1997 - Fixed macro, function names      */
  21. /*  Version 1.1.0 - 29th January 1998 - New OS function names        */
  22. /*  Version 1.1.1 - 19th June 1998 - Casts for InitialiseMemory      */
  23. /*  Version 1.2.0 - 20th November 1998 - Avoids C/C++ warnings       */
  24. /*  Version 1.4.0 - 26th May 1999 - Added linked list routines       */
  25. /*                                                                   */
  26. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  27.  
  28. #include "Essential Headers.h"
  29. #include "Essential Prototypes.h"
  30.  
  31. void AddToLinkedList(void** firstPointer, void* addItem, void* beforeItem)
  32. {
  33.     void    **slotPointer;
  34.  
  35.     slotPointer=firstPointer;
  36.     while (1) {
  37.  
  38.         if (*slotPointer==beforeItem) {
  39.             *(void**)addItem=beforeItem;
  40.             *slotPointer=addItem;
  41.             break;
  42.         }
  43.         
  44.         Assert(*slotPointer);
  45.         if (*slotPointer==0)
  46.             break;
  47.         slotPointer=(void**)*slotPointer;
  48.     }
  49. }
  50.  
  51. void RemoveFromLinkedList(void** firstPointer, void* removeItem)
  52. {
  53.     void    **slotPointer;
  54.  
  55.     slotPointer=firstPointer;
  56.     while (1) {
  57.  
  58.         Assert(*slotPointer);
  59.         if (*slotPointer==0)
  60.             break;
  61.  
  62.         if (*slotPointer==removeItem) {
  63.             *slotPointer=*(void**)removeItem;
  64.             break;
  65.         }
  66.         
  67.         slotPointer=(void**)*slotPointer;
  68.     }
  69. }
  70.  
  71. void* GetLinkedListItem(void* firstPointer, long itemNumber)
  72. {
  73.     void    *itemPointer;
  74.     
  75.     itemPointer=firstPointer;
  76.     while(itemNumber) {
  77.     
  78.         itemPointer=*(void**)itemPointer;
  79.         Assert(itemPointer);
  80.         if (itemPointer==0)
  81.             break;
  82.  
  83.         itemNumber--;
  84.     }
  85.     
  86.     return itemPointer;
  87. }
  88.  
  89. long CountLinkedListItems(void* firstPointer)
  90. {
  91.     void    *itemPointer;
  92.     long    count;
  93.     
  94.     count=0;
  95.     itemPointer=firstPointer;
  96.     while (itemPointer) {
  97.         count++;
  98.         itemPointer=*(void**)itemPointer;
  99.     }
  100.     
  101.     return count;
  102. }
  103.  
  104. Error CreateHeapSpace(Size required)
  105. {
  106.     Error    error=0;
  107.     Size    allocate;
  108.     
  109.     allocate=required+Emergency_Memory_Reserve;
  110.     
  111.     if (MaxBlock()<allocate) {
  112.         PurgeMem(allocate);
  113.         if (MaxBlock()<allocate) {
  114.             MyFreeUpMemory(allocate);
  115.             
  116.             if (MaxBlock()<allocate)
  117.                 error=memFullErr;
  118.         }
  119.     }
  120.     
  121.     TestError(error);
  122.     return error;
  123. }
  124.  
  125. void*    AllocPtr(Error* error, Size allocate)
  126. {
  127.     void*    pointer;
  128.     
  129.     *error=CreateHeapSpace(allocate);
  130.         _i(*error)
  131.         
  132.     pointer=(void*)NewPtr(allocate);
  133.     *error=TestMemError(pointer);
  134.         _i(*error);
  135.         
  136.     #if Project_Under_Development && Initialise_Allocated_Memory
  137.         InitialiseMemory((char*)pointer, allocate);
  138.     #endif    
  139.  
  140.     return pointer;
  141.     _e
  142.     return 0;
  143. }
  144.  
  145. void**    AllocHandle(Error* error, Size allocate)
  146. {
  147.     void**    handle;
  148.     
  149.     *error=CreateHeapSpace(allocate);
  150.         _i(*error)
  151.         
  152.     handle=(void**)NewHandle(allocate);
  153.     *error=TestMemError(handle);
  154.         _i(*error);
  155.         
  156. #if Project_Under_Development
  157. #if Move_On_Handle_Allocation
  158.     MoveHHi((Handle)handle);
  159. #endif
  160.  
  161. #if Initialise_Allocated_Memory
  162.     InitialiseMemory((char*)*handle, allocate);
  163. #endif
  164. #endif    
  165.  
  166.     return handle;
  167.     _e
  168.     return 0;
  169. }
  170.  
  171. void    DestroyPtr(void* pointer)
  172. {
  173. #if Project_Under_Development && Initialise_Allocated_Memory
  174.     InitialiseMemory((char*)pointer, GetPtrSize((Ptr)pointer));
  175. #endif    
  176.     DisposePtr((Ptr)pointer);
  177. }
  178.  
  179. void    DestroyHandle(void** handle)
  180. {
  181. #if Project_Under_Development && Initialise_Allocated_Memory
  182.     InitialiseMemory((char*)*handle, GetHandleSize((Handle)handle));
  183. #endif    
  184.     DisposeHandle((Handle)handle);
  185. }
  186.  
  187. #if Project_Under_Development
  188.  
  189. #if Assert_Memory_Locking
  190.  
  191. // I'm sure the next two routines could be written better with more knowledge of
  192. // Apple's System Software, but the programming guidelines don't offer such
  193. // information, so it's likely to change. Thus I do this the long way round.
  194.  
  195. void    LockHandleAssert(void** handle)
  196. {
  197.     SignedByte    statusBefore, statusAfter;
  198.     
  199.     statusBefore=HGetState((Handle)handle);
  200.     HLock((Handle)handle);
  201.     statusAfter=HGetState((Handle)handle);
  202.     
  203.     Assert(statusBefore!=statusAfter);
  204. }
  205.  
  206. void    UnlockHandleAssert(void** handle)
  207. {
  208.     SignedByte    statusBefore, statusAfter;
  209.     
  210.     statusBefore=HGetState((Handle)handle);
  211.     HUnlock((Handle)handle);
  212.     statusAfter=HGetState((Handle)handle);
  213.     
  214.     Assert(statusBefore!=statusAfter);
  215. }
  216.     
  217. #endif
  218.  
  219. #if Initialise_Allocated_Memory
  220.  
  221. // This could be optimised by first filling with long words of 0xAAAAAAAA, but there's
  222. // little point as it's only used when the project is under development anyway.
  223.  
  224. void    InitialiseMemory(char* pointer, Size length)
  225. {
  226.     char    *trasher, *topAddress;
  227.     
  228.     trasher=pointer;
  229.     topAddress=trasher+length;
  230.     
  231.     while (trasher<topAddress)
  232.         *trasher++=(char)0xAA;
  233. }
  234.     
  235. #endif
  236.  
  237. #endif
  238.